import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import home from '@/views/home/home.vue'
import category from '@/views/category/category.vue'
import shopcart from '@/views/shopcart/shopcart.vue'
import user from '@/views/user/user.vue'
import productList from '@/views/product-list/product-list.vue'
import productDetail from '@/views/product-detail/product-detail.vue'
import login from '@/views/login/login.vue'
import register from '@/views/login/register.vue'
import profile from "@/views/profile/profile.vue";
import store from "@/store";


const routes = [{
        path: '/',
        redirect: '/home',
        component: () =>
            import ('@/views/home/home.vue'),
        meta: {
            index: 1
        }
    }, {
        path: '/home',
        name: home,
        component: () =>
            import ('@/views/home/home.vue'),
        meta: {
            index: 1
        }
    }, {
        path: '/product-list/:id',
        name: productList,
        component: () =>
            import ('@/views/product-list/product-list.vue'),
        meta: {
            index: 2
        }
    }, {
        path: '/product/:id',
        name: productDetail,
        component: () =>
            import ('@/views/product-detail/product-detail.vue'),
        meta: {
            index: 2
        }
    }, {
        path: '/login',
        name: login,
        component: () =>
            import ('@/views/login/login.vue'),
        meta: {
            index: 2
        }
    }, {
        path: '/register',
        name: register,
        component: () =>
            import ('@/views/login/register.vue'),
        meta: {
            index: 2
        }
    }, {
        path: '/category',
        name: category,
        component: () =>
            import ('@/views/category/category.vue'),
        meta: {
            index: 1
        }
    }, {
        path: '/shopcart',
        name: shopcart,
        component: () =>
            import ('@/views/shopcart/shopcart.vue'),
        meta: {
            index: 1,
            auth: true
        }
    }, {
        path: '/user',
        name: user,
        component: () =>
            import ('@/views/user/user.vue'),
        meta: {
            index: 1,
            auth: true
        }
    }, {
        path: '/profile',
        name: profile,
        component: () =>
            import ('@/views/profile/profile.vue'),
        meta: {
            index: 2
        }
    },
    {
        path: '/proxy',
        name: 'Proxy',
        component: () =>
            import ('@/views/login/proxy.vue')
    }

];

const router = new VueRouter({
    mode: 'history',
    routes,
});

router.beforeEach((to, from, next) => {
    // 判断当前用户 已登录 再次访问 登录界面 直接跳转到 个人中心页面
    if (to.name === 'login' && store.getters.getIsLogin) {
        next('/user')
    }
    if (to.meta.auth) { // 需要校验用户是否登录的页面
        // 判断 用户是否登录
        if (store.getters.getIsLogin) {
            next()
        } else {
            // 未登录 跳转到登录页面
            next({ path: '/login' })
        }
    } else {
        next()
    }
})
export default router;